package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.CloudContact;
import com.lzx.iteam.bean.ContactNameCard;
import com.lzx.iteam.bean.PhoneNumItem;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PhoneNumberArea;
import com.lzx.iteam.util.PhoneNumberUtils;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by hfx on 15/3/10.
 */
public class GetContactNameCardMsg extends CcMsgStructure {
    private Context mmContext;
    private Message mmCallback;
    private PhoneNumberArea mPhoneNumberArea;

    public GetContactNameCardMsg(Context context, Message message) {
        mmContext = context;
        mmCallback = message;
        mPhoneNumberArea = PhoneNumberArea.getInstance(mmContext);

    }

    @Override
    public void onReceive(JSONObject jsonObject) {
        ContactNameCard contactNameCard=new ContactNameCard();
        try {
            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
            JSONObject userInfo=jsonObject1.getJSONObject("user_info");
            contactNameCard.userName=userInfo.getString(AsynHttpClient.KEY_USER_NAME);
            contactNameCard.avatar=userInfo.getString(AsynHttpClient.KEY_AVATAR);
            contactNameCard.sex=userInfo.getInt(AsynHttpClient.KEY_SEX);
            JSONArray jsonArray = jsonObject1.getJSONArray("groups");
            for (int i = 0; i < jsonArray.length(); i++) {
                CloudContact contact = new CloudContact();
                JSONObject jContact = jsonArray.getJSONObject(i);
                contact.contactId = jContact.getString(AsynHttpClient.KEY_CONTACT_ID);
                contact.groupId = jContact.getString(AsynHttpClient.KEY_GROUP_ID);
                contact.groupName = jContact.getString(AsynHttpClient.KEY_GROUP_NAME);
                contact.contactName = jContact.getString(AsynHttpClient.KEY_CONTACT_NAME);
                if(jContact.has("contact_phone")){
                contact.contact_phone = PhoneNumberUtils.cutPlus86Formatter(jContact.getString(AsynHttpClient.KEY_CONTACT_PHONE));}
                contact.contact_card = jContact.getJSONObject(AsynHttpClient.KEY_CONTACT_CLOUD_NAMECARD).toString();
               // contact.contact_sex = jContact.getInt(AsynHttpClient.KEY_SEX);
                //contact.userId = jContact.getString(AsynHttpClient.KEY_USER_ID);
                //contact.userType = jContact.getString(AsynHttpClient.KEY_USER_TYPE);
                //contact.contactRemarks = jContact.getString(AsynHttpClient.KEY_CONTACT_REMARKS);
                //  contact.contact_image = jContact.getString(AsynHttpClient.KEY_CONTACT_IMAGE);
                if (jContact.has("phone_purview")) {
                    contact.phone_purview = jContact.getString(AsynHttpClient.KEY_PHONE_PURVIEW);
                } else {
                    contact.phone_purview = "1";
                }
                contact.contactPhoneNum = new ArrayList<PhoneNumItem>();
/*           String jNumListStr = jContact.getString(AsynHttpClient.KEY_CONTACT_PHONENUMBER);
            if (!TextUtils.isEmpty(jNumListStr)) {
                JSONArray jNumListArr = new JSONArray(jNumListStr);
                for (int j = 0; j < jNumListArr.length(); j += 2) {
                    PhoneNumItem phoneItem = new PhoneNumItem();
                    phoneItem.label = jNumListArr.getString(j);
                    phoneItem.phoneNum = jNumListArr.getString(j + 1);
                    contact.contactPhoneNum.add(phoneItem);
                }
            }
*/
                //contact.contact_landline = jContact.getString(AsynHttpClient.KEY_CONTACT_LANDLINE);

                if (!StringUtil.isEmpty(contact.contact_phone)) {
                    PhoneNumItem item = new PhoneNumItem();
                    item.phoneNum = contact.contact_phone;
                    /*String area = mPhoneNumberArea.getArea(contact.contact_phone, true);
                    if (!StringUtil.isEmpty(area)) {
                        item.label = area;
                    } else {
                        item.label = "未知";
                    }*/
                    item.label = "";
                    contact.contactPhoneNum.add(item);
                }
                contactNameCard.groups.add(contact);
            }
            /*if (!StringUtil.isEmpty(contact.contact_landline)){
                PhoneNumItem item = new PhoneNumItem();
                item.phoneNum = contact.contact_landline;
                String area = mPhoneNumberArea.getArea(contact.contact_landline, true);
                if (!StringUtil.isEmpty(area)){
                    item.label = area;
                }else{
                    item.label = "未知";
                }
                contact.contactPhoneNum.add(item);
            }*/

        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (mmCallback != null) {
            mmCallback.arg1 = 0;
            mmCallback.obj = contactNameCard;
            mmCallback.sendToTarget();
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
